(function() {
	'use strict';

	var filename = "templates/radio/radioList.html";

	// var templateHTML = gm.io.readFile(filename);

	var templateHTML = '<div class="radioListBar">'
			+ '{{#each channellist}}<div class="channel" channelid="{{this.channelid}}"><span>{{this.name}}</span></div>{{/each}}'
			+ '</div>'
			+ '<div id="leftArrowBtn" class="leftArrowBtn btn"><</div>'
			+'<div id="selectChannel" class="selectChannelBtn"></div>'
			+'<div id="rightArrowBtn" class="rightArrowBtn btn">></div>';

	// test two

	CCR.Views.ChannelListView = Backbone.View
			.extend({
				el : '#radioList', // 模板根元素
				template : Handlebars.compile(templateHTML), // 模板
				hidden : true, // 是否显示
				default_channel_i : 0, // 当前频道
				default_song_i : 0, // 当前歌曲编号
				channelinfo : null, // 频道信息:{channel_id,name,songlist}
				songinfo : null, // 歌曲信息:{songurl}
				currentMediaHandle : null,// 当前播放的歌曲句柄
				mediaStatus : null,
				position : 0,
				songlist : [],
				songlrc : null,
				// 时间更新及时期
				updatetimeTimer : null,

				// 事件绑定
				events : {
				/*
				 * "rotarySelect .radioListBar div" :
				 * "channelButtonRotarySelect", "rotaryBlur .radioListBar div" :
				 * "channelButtonRotaryBlur", "rotaryFocus .radioListBar div" :
				 * "channelButtonRotaryFocus",
				 */
				// debug view
					/*
				"rotaryBlur .btn" : "buttonRotaryBlur",
					"rotaryFocus .btn" : "buttonRotaryFocus",
					"rotarySelect .btn" : "buttonRotarySelect",
					"mouseup .btn" : "buttonRotarySelect", 
					*/
				},
				// 事件处理函数：进入焦点
				buttonRotaryFocus : function(e) {
					$(e.target).addClass("focus");
				},
				buttonRotaryBlur : function(e) {
					$(e.target).removeClass("focus");
				},
				buttonRotarySelect : function(e) {
					var self = this;
					$(e.target).addClass("selected");
					setTimeout(function() {
						$(e.target).removeClass("selected");
						var targetId = $(e.target).attr("id");
						switch (targetId) {
							case "leftArrowBtn":
								self.Channel.toLeft();
							break;
							case "rightArrowBtn":
								self.Channel.toRight();
							break;
							case "selectChannel":
								self.Channel.toSelect(self.Channel.currentIndex);
							break;
						}
					}, 100);
				},
				Channel : {
					channelList : [],
					currentIndex : 0,
					width : 130,// 频道标题宽度
					// 频道选择 - 左选
					toLeft : function() {
						if (this.currentIndex >= 0) {
							this.toIndex(--this.currentIndex);
						}else{
							this.currentIndex = this.channelList.length-1;
							this.toIndex(this.currentIndex);
						}
					},
					// 频道选择 - 右选
					toRight : function() {
						if (this.currentIndex < this.channelList.length - 1) {
							this.toIndex(++this.currentIndex);
						}else{
							this.currentIndex = 0;
							this.toIndex(this.currentIndex);
						}
					},
					toIndex : function(index) {
						var self = this;
						if (index < 0 || index >= this.channelList.length) {
							// Exception
							// throw "Channel.toIndex(index) index参数超出范围";
						}
						else {
							this.currentIndex = index;
//							$(".radioListBar").css("margin-left",-65 + this.width * this.currentIndex * -1 + "px");
							var channellist = self.getCenterChannel();
							self.changeCenterChannel(channellist);
						}
						
					},
					getCenterChannel:function(){
						var number = 5;//设定需要获取的中间段长度
						var self = this;
						var concatChannellist = new Array();
						concatChannellist = self.channelList.concat(self.channelList).concat(self.channelList);
						var index = self.currentIndex+self.channelList.length-Math.floor(number/2);
						var channellist = new Array();
						for(var i=0;i<number;i++){
							channellist.push(concatChannellist[index+i]);
						}
						return channellist;
					},
					changeCenterChannel:function(channel_list){
						for(var i=0;i<channel_list.length;i++){
//							<div class="channel" channelid="{{this.channelid}}"><span>{{this.name}}</span></div>
							if(i===Math.floor(channel_list.length/2)){
								var className = $($(".channel")[i]).attr("class");
								if(className.indexOf("currentChannel")==-1){
									$($(".channel")[i]).addClass("currentChannel");
								}
							}
							$($($(".channel")[i]).attr("channelid",channel_list[i].channelid).children("span")).html(channel_list[i].name);
							
						}
					},
					// 频道选额 - 选中
					toSelect : function(index) {
						var self = this;
						if (index && index != 0) { // 判断channelId有效
							// View.playList(channelId);
						}
						else { // channelId无效时默认选择channelList第一个频道
							index = Math.floor(self.channelList.length/2);
							self.toIndex(index);
							// View.playList(this.channelList[0]);
						}
						;
						this.curretnIndex = index;
//						var target = this.getTargetByIndex(index);
//						$(".radioListBar div").removeClass("selectedChannel");
//						target.addClass("selectedChannel");
						// 获取频道列表
						var channelId = this.channelList[index].channelid;
						// customer用户选择频道记录
						CCR.Customes.addChannelId(channelId);
						
						if(channelId!="-999"){
							CCR.SongList.getSongList(channelId, function(channelInfo) {
								CCR.SongList.playNext();
							});
						}else{
							log("选择了红心频道");
							//获取红心频道歌曲列表
							CCR.Favorites.getFavSong(function(songList) {
								CCR.Songs.channelInfo_id = "-999";
								CCR.Songs.channelInfo_name = "红心";
								//重置播放歌曲列表
								CCR.Songs.songList = songList;
								//重置播放序列号
								CCR.Songs.song_i = -1;
								if(songList.length>0){
									CCR.Songs.playNext(CCR.Songs);
								}else{
									log("没有收藏歌曲");
								}
							});
						}
						
						
					},
					getTargetByIndex : function(index) {
						return $(".radioListBar div[channelid="
								+ this.channelList[index].channelid + "]");
					},
					getChannelList:function(callback){
						var radio_catalog_url = CCR.Config.getCatalogUrl();
						var self = this;
						CCR.Catalogs = new CCR.Collections.Catalogs(
								{ url : radio_catalog_url });
						CCR.Catalogs.fetch({ success : function() {
								var results = CCR.Catalogs.toJSON();
								self.channelList = results[0].channellist;
								typeof callback==="function"&&callback();
							}
						});
					}
				},
				// 显示歌词信息
				showLrc : function(songinfo, s) {

					var self = s;

					var url = songinfo.songinfo.lrclink;

					$.ajax({ type : "get", async : true, url : url,
						cache : false, success : function(data) {
							if (data) {
								self.songlrc = data.split('\n');
							}

						}, error : function(err) {
							// alert(err);
						} });
				},

				// 显示
				show : function() {
					this.hidden = false;
					this.render();
				},

				// 隐藏
				hide : function() {
					if (!this.hidden) {
						this.hidden = true;
						this.$el.html("");
					}
				},

				// 渲染
				render : function() {
					//var results = this.collection.toJSON();
					var self = this;
					self.Channel.getChannelList(function(){
						if(self.template) {
							//初始化取中间5个频道
							var number = 5;
							var index = Math.floor(self.Channel.channelList.length/2)-Math.floor(5/2);
							var centerChannelList = new Array();
							for(var i=0;i<number;i++){
								centerChannelList.push(self.Channel.channelList[index]);
								++index;
							}
							self.$el.html(self
									.template({ channellist : centerChannelList }));
							// 选择频道
							self.Channel.toSelect(); // 默认选择第一个频道
							/*
							 * var count = 0; // 计时器 self.updatetimeTimer =
							 * $.timer(function() {
							 * 
							 * if (!self.songlrc) { return; }
							 *  // 显示歌词 count >= self.songlrc.length ?
							 * self.songlrc.length - 1 : count++;
							 * 
							 * $("#lrc").html(self.songlrc[count]);
							 *  }, 1000, false);
							 */
							//下一首
							$("#radioList .btn").unbind("rotaryBlur").bind("rotaryBlur",function(){
								var t = this;
								$(t).removeClass("focus");
								var targetId = $(t).attr("id");
								switch (targetId) {
									case "leftArrowBtn":
										self.Channel.toLeft();
									break;
									case "rightArrowBtn":
										self.Channel.toRight();
										break;
								}
							}).unbind("rotaryFocus").bind("rotaryFocus",function(){
								var t = this;
								$(t).addClass("focus");
								var targetId = $(t).attr("id");
								switch (targetId) {
									case "leftArrowBtn":
										self.Channel.toLeft();
									break;
									case "rightArrowBtn":
										self.Channel.toRight();
										break;
								}
							}).unbind("rotarySelect mouseup").bind("rotarySelect mouseup",function(){
								var t = this;
								$(t).addClass("selected");
								setTimeout(function() {
									$(t).removeClass("selected");
									var targetId = $(t).attr("id");
									switch (targetId) {
										case "leftArrowBtn":
											//self.Channel.toLeft();
											self.Channel.toSelect(self.Channel.currentIndex);
										break;
										case "rightArrowBtn":
											//self.Channel.toRight();
											self.Channel.toSelect(self.Channel.currentIndex);
										break;
										case "selectChannel":
											self.Channel.toSelect(self.Channel.currentIndex);
										break;
									}
								}, 100);
								
							});
							
							Rotary.fresh();	//绑定旋钮fresh
						};
					});
					return this;
				},

				initialize : function() {
					this.render();
				} });

})();